package com.feature.core.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import com.feature.core.webmvc.RedisSessionProperties;

@EnableRedisHttpSession
@Configuration
@EnableConfigurationProperties(RedisSessionProperties.class)
public class RedisSessionConfig {

	@Bean
	SessionRepositoryCustomizer<RedisIndexedSessionRepository> setSessionRepositoryCustomizer(RedisSessionProperties config) {
		return new SessionRepositoryCustomizer<RedisIndexedSessionRepository>() {

			@Override
			public void customize(RedisIndexedSessionRepository sessionRepository) {
				if (config.getFlushMode() != null) {
					sessionRepository.setFlushMode(config.getFlushMode());
				}
				if (StringUtils.isNotBlank(config.getNamespace())) {
					sessionRepository.setRedisKeyNamespace(config.getNamespace());
				}
				if (config.getTimeout() != null) {
					sessionRepository.setDefaultMaxInactiveInterval(config.getTimeout());
				}
			}
		};
	}
}
